﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using HouseAgencySite.Models;
using HouseAgencySite.Utils;
using Webdiyer.WebControls.Mvc;

namespace HouseAgencySite.Areas.Admin.Controllers
{
    public class UsersController : BaseController
    {
        private AgencyDBContext db = new AgencyDBContext();

        //
        // GET: /Admin/Users/
         [CheckLogin()]
        public ActionResult Index(int pageindex = 1)
        {
            List<Users> list = db.Users.Where(o=>o.IsDelete==false).ToList();
            IPagedList pageList = list.ToPagedList(pageindex, 10);
            return View(pageList);
        }

        //
        // GET: /Admin/Users/Details/5

        public ActionResult Details(string id = null)
        {
            Users users = db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View(users);
        }

        //
        // GET: /Admin/Users/Create
           [CheckLogin()]
        public ActionResult Create()
        {
            Users users = new Users();
            users.Guid = Guid.NewGuid().ToString();
            return View(users);
        }

        //
        // POST: /Admin/Users/Create

        [HttpPost]
        [CheckLogin()]
        public ActionResult Create(Users users)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    users.ImgUrl = Request["ImgUrl"];
                    users.UserPwd = Md5Helper.ToMD5(users.UserPwd);
                    users.ConfirmPwd = Md5Helper.ToMD5(users.ConfirmPwd);
                    users.Create(users, CurrentUser);
                    db.Users.Add(users);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(users);
            }
            catch (DbEntityValidationException dbEx)
            {
                return View();
            }
        }

        //
        // GET: /Admin/Users/Edit/5
           [CheckLogin()]
        public ActionResult Edit(string id = null)
        {
            Users users = db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View(users);
        }

        //
        // POST: /Admin/Users/Edit/5

        [HttpPost]
        [CheckLogin()]
        public ActionResult Edit(Users users)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    users.UserPwd = Md5Helper.ToMD5(users.UserPwd);
                    users.ConfirmPwd = Md5Helper.ToMD5(users.ConfirmPwd);
                    db.Entry(users).State = EntityState.Modified;
                    users.Edit(db, users, CurrentUser);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(users);
            }
            catch (DbEntityValidationException dbEx)
            {
                return View();
            }
        }

        //
        // GET: /Admin/Users/Delete/5
        [CheckLogin()]
        public ActionResult Delete(string id = null)
        {
            Users users = db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View(users);
        }

        //
        // POST: /Admin/Users/Delete/5
        [CheckLogin()]
        [HttpPost, ActionName("Delete")]
        public int DeleteConfirmed(string id)
        {
            Users users = db.Users.Find(id);
            db.Entry(users).State = EntityState.Modified;
            users.Delete(db, users, CurrentUser);
            return db.SaveChanges();
            //return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="userFilter"></param>
        /// <returns></returns>
        [CheckLogin()]
        public ActionResult Search(UserSearchFilter userFilter)
        {
            var list = db.Users.Where(o => o.IsDelete == false);
            if (!string.IsNullOrEmpty(userFilter.LogonName))
            {
                list = list.Where(o => o.LogonName.Contains(userFilter.LogonName));
            }
            if (!string.IsNullOrEmpty(userFilter.UserName))
            {
                list = list.Where(o => o.UserName.Contains(userFilter.UserName));
            }
            if (!string.IsNullOrEmpty(userFilter.Telephone))
            {
                list = list.Where(o => o.Telephone.Contains(userFilter.Telephone));
            }
            if (!string.IsNullOrEmpty(userFilter.Email))
            {
                list = list.Where(o => o.Email.Contains(userFilter.Email));
            }
            int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
            return View("Index", list.ToList().ToPagedList(pageIndex, 10));
        }
        [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]   //清除缓存
        public JsonResult CheckLogonName(string logonName, string guid)
        {
            if (!string.IsNullOrEmpty(logonName))
            {
                if (string.IsNullOrEmpty(guid) || guid == "undefined")
                {
                    if (db.Users.ToList().Any(o => o.LogonName.ToLower() == logonName.ToLower() && o.IsDelete == false)) return Json(false, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    if (db.Users.ToList().Any(o => o.LogonName.ToLower() == logonName.ToLower() && o.Guid != guid && o.IsDelete == false)) return Json(false, JsonRequestBehavior.AllowGet);
                }
            }
            return Json(true, JsonRequestBehavior.AllowGet); ;
        }

    }
}